package com.pig4cloud.pig.module.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.DateUtils;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.MerTable;
import com.pig4cloud.pig.module.api.entity.product.ProductOrderTable;
import com.pig4cloud.pig.module.service.mer.IMerTableService;
import com.pig4cloud.pig.module.service.product.IProductOrderTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 桌位订单Controller
 *
 * @author ruoyi
 * @date 2023-08-26
 */
@Validated
@Api(value = "桌位订单控制器", tags = {"桌位订单管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("module/productOrderTable")
public class ProductOrderTableController {

    private final IProductOrderTableService productOrderTableService;
    private final IMerTableService tableService;


    /**
     * 查询桌位订单列表
     */
    @ApiOperation("查询桌位订单列表")
    @PreAuthorize("@pms.hasPermission('module:productOrderTable:list')")
    @GetMapping("/list")
    public TableDataInfo<ProductOrderTable> list(@Validated ProductOrderTable productOrderTable) {
        QueryWrapper<ProductOrderTable> qw = new QueryWrapper<>();

        TableDataInfo<ProductOrderTable> td = PageUtils.buildDataInfo(productOrderTableService.page(PageUtils.buildPage(),qw));
        List<ProductOrderTable> list = td.getData();
        for (ProductOrderTable list1:list){
            if (list1.getTableId()!=null){
                MerTable table = tableService.getById(list1.getTableId());
                if (table!=null){
                    list1.setTableName(table.getTableName());
                }
            }
        }
        return td;
    }

    /**
     * 导出桌位订单列表
     */
    @ApiOperation("导出桌位订单列表")
    @PreAuthorize("@pms.hasPermission('module:productOrderTable:export')")
    @SysLog("桌位订单")
    @GetMapping("/export")
    public List export(@Validated ProductOrderTable productOrderTable) {
        List<ProductOrderTable> list = productOrderTableService.list();
        for (ProductOrderTable list1 :list){
            if (list1.getTableId()!=null){
                MerTable table = tableService.getById(list1.getTableId());
                if (table!=null){
                    list1.setTableName(table.getTableName());
                }
            }
            if (list1.getPayTime()!=null){
                list1.setPayTimes(DateUtils.getTime(list1.getPayTime()));
            }
        }
        // ExcelUtil<ProductOrderTable> util = new // ExcelUtil<ProductOrderTable>(ProductOrderTable.class);
        return  list;// util.exportExcel(list, "桌位订单");
    }

    /**
     * 获取桌位订单详细信息
     */
    @ApiOperation("获取桌位订单详细信息")
    @PreAuthorize("@pms.hasPermission('module:productOrderTable:query')")
    @GetMapping("/{id}")
    public R<ProductOrderTable> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        ProductOrderTable productOrderTable = productOrderTableService.getById(id);
        return R.ok(productOrderTable);
    }

    /**
     * 新增桌位订单
     */
    @ApiOperation("新增桌位订单")
    @PreAuthorize("@pms.hasPermission('module:productOrderTable:add')")
    @SysLog("桌位订单")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ProductOrderTable productOrderTable) {
        return R.okByRow(productOrderTableService.save(productOrderTable) ? 1 : 0);
    }

    /**
     * 修改桌位订单
     */
    @ApiOperation("修改桌位订单")
    @PreAuthorize("@pms.hasPermission('module:productOrderTable:edit')")
    @SysLog("桌位订单")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ProductOrderTable productOrderTable) {
        return R.okByRow(productOrderTableService.updateById(productOrderTable) ? 1 : 0);
    }

    /**
     * 删除桌位订单
     */
    @ApiOperation("删除桌位订单")
    @PreAuthorize("@pms.hasPermission('module:productOrderTable:remove')")
    @SysLog("桌位订单")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(productOrderTableService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
